package socket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

public class ServerTest {
    private ServerSocket serverSocket;

    //private PrintWriter[] allOut = {};//线程共享数组
    private Collection<PrintWriter> allOut = new ArrayList<>();

    public ServerTest(){
        try {
            System.out.println("正在启动服务器...");
            serverSocket = new ServerSocket(8088);
            System.out.println("服务器启动完毕！！！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void start(){
        try {
            //接受来自客户端的连接
            while (true) {
                Socket socket = serverSocket.accept();
                //启动一个线程负责处理该客户端交互
                ClientHandler handler = new ClientHandler(socket);
                Thread t = new Thread(handler);
                t.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args){
        ServerTest serverTest = new ServerTest();
        serverTest.start();
    }


    //该线程任务负责与指定客户端进行交互
    private class ClientHandler implements Runnable{
        private Socket socket;
        private String host; // 记录对应客户端的ip地址

        public ClientHandler(Socket socket){
            this.socket = socket;
            //通过socket获取远端计算机IP地址（获取到客户端）
            host = socket.getInetAddress().getHostAddress();
        }

        public void run() {
            PrintWriter pw = null;
            try {
                InputStream ips = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(ips, StandardCharsets.UTF_8);
                BufferedReader br = new BufferedReader(isr);

                OutputStream out = socket.getOutputStream();
                OutputStreamWriter osw = new OutputStreamWriter(out,StandardCharsets.UTF_8);
                BufferedWriter bw = new BufferedWriter(osw);
                pw = new PrintWriter(bw,true);

                synchronized (ServerTest.this) {
                    allOut.add(pw);
                }
                message(host+"上线了,当前连接数:"+allOut.size());

                String line;
                while ((line = br.readLine()) != null) {
                    message(line);
                }
            }catch (IOException e){
                e.printStackTrace();
            }finally {
                try {
                    synchronized (ServerTest.this) {
                        allOut.remove(pw);
                    }
                    message(host+"下线了,当前连接数:"+allOut.size());
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        private void message(String message){
            System.out.println(host+"说:"+ message);
            synchronized (ServerTest.this) {
                for (PrintWriter data : allOut) {
                    data.println(host + "说:" + message);
                }
            }
        }
    }

}